package com.bicon.botu.finance.payment.service;

import com.alipay.api.internal.util.StringUtils;
import com.bicon.botu.core.base.common.BusinessException;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.finance.payment.alipay.AliPayService;
import com.bicon.botu.finance.payment.common.PaymentConstants;
import com.bicon.botu.finance.payment.entity.*;
import com.bicon.botu.finance.payment.unionpay.UnionPayService;
import com.bicon.botu.finance.payment.weixin.api.WeChatPayService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @Author: 宋旭
 * @Date: Created in 10:58 2017/8/1/001
 */
@Service
@Log4j
public class PayService {

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private WeChatPayService weChatPayService;

    @Autowired
    private UnionPayService unionPayService;

    @Autowired
    private PayInfoService payInfoService;


    /**
     * 支付接口
     *
     * @param infoEntity  第三方支付信息
     * @param orderNum    订单号
     * @param subjectName 支付标题
     * @param body        支付信息体 微信支付时，可以忽略
     * @param money       支付金额
     * @param type        支付类型 1:支付宝 2:微信 3:银联
     * @param attr        扩展字段，传入什么，支付成功后传回什么
     * @return
     */
    public BaseResult launchPay(AppPaymentInfoEntity infoEntity, String orderNum, String subjectName, String body,
                                String money, String type, String attr) {
        //金额参数校验
        float total = 0.0f;
        try {
            total = Float.parseFloat(money);
        } catch (Exception e) {
            log.info("error", e);
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, "金额异常", null);
        }
        String message = null;
        Date date = new Date();
        if (type.equals(AliPayDto.TYPE_ALIPAY)) {
            //支付宝支付
            if (StringUtils.isEmpty(body)) {
                return new BaseResult(PaymentConstants.RESPONSE_CODE_400, "支付信息体不能为空", null);
            }
            //获取app在支付宝平台的相关信息
            AliPayDto aliPayDto = infoEntity.getAliPayDto();
            if (null == aliPayDto) {
                return new BaseResult(PaymentConstants.RESPONSE_CODE_400, "支付宝支付参数为空", null);
            }
            //预支付
            message = aliPayService.appPay(infoEntity.getCode(), aliPayDto.getAlipayAppId(), aliPayDto.getAlipayPublicKey(),
                    aliPayDto.getAlipayAppPrivateKey(), orderNum, subjectName, body, money);
        } else if (type.equals(WechatPayDto.TYPE_WECHAT)) {
            //微信支付
            WechatPayDto wechatPayDto = infoEntity.getWechatPayDto();
           if (null == wechatPayDto) {
                return new BaseResult(PaymentConstants.RESPONSE_CODE_400, "微信支付参数为空", null);
            }
            message = weChatPayService.payToWeChat(infoEntity.getCode(), wechatPayDto.getWechatAppId(), wechatPayDto.getWechatAppPartner(),
                    wechatPayDto.getWechatAppKey(), wechatPayDto.getWechatAppCertPath(), orderNum, subjectName, total);
        } else if (type.equals(UnionpayDto.TYPE_UNION)) {
            UnionpayDto unionpayDto = infoEntity.getUnionpayDto();
            if (null == unionpayDto) {
                return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400, null);
            }
            message = unionPayService.payToUnion(unionpayDto, orderNum, infoEntity.getCode(), money, date, infoEntity.getNotifyUrl());
        } else {
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, PaymentConstants.RESPONSE_MESSAGE_400, null);
        }
        if (null != message) {
            payInfoService.savePayInfo(infoEntity.getCode(), orderNum, money, type, attr, date);
            return new BaseResult(PaymentConstants.RESPONSE_CODE_200, PaymentConstants.RESPONSE_MESSAGE_200, message);
        }
        return new BaseResult(PaymentConstants.RESPONSE_CODE_600, "系统异常", null);
    }

}
